<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class SettingRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $editRules = [
            'id'       => 'required|numeric',
            'name'     => 'required',
            'variable' => 'required|alpha_dash',
            'value'    => 'required',
            'sort'     => 'required|numeric',
            'system'   => 'required|boolean',
        ];

        $createRules = [
            'name'     => 'required',
            'variable' => 'required|alpha_dash',
            'value'    => 'required',
            'sort'     => 'required|numeric',
            'system'   => 'required|boolean',
        ];

//        return array();

        $data = Request::all();
        //如果是编辑
        if ($data['formType'] == 'edit') {
            return $editRules;
        }
        //如果是创建
        if ($data['formType'] == 'create') {
            return $createRules;
        }
    }

    public function attributes()
    {
        $attributes = array(
            'name'     => '系统参数名称',
            'variable' => '英文名',
            'sort'     => '排序',
            'system'   => '是否系统变量',
        );

        return $attributes;
    }

    public function messages()
    {
        $messages = [
            'required'       => ':attribute为必填选项',
            'numeric'        => ':attribute必须为数字',
            'alpha_dash'     => ':attribute必须为字母和数字，以及破折号或下划线',
            'system.boolean' => ':attribute必须为是、不是中的一种',
        ];

        return $messages;
    }
}
